#nullable enable

using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services;

namespace PHMEE.EnergyEfficiency;

/// <summary>
/// 航程计算器
/// </summary>
public class DistanceCalculator : DomainService
{
    private readonly IGpsTdRepository _gpsTdRepository;

    public DistanceCalculator(IGpsTdRepository gpsTdRepository)
    {
        _gpsTdRepository = gpsTdRepository;
    }
    
    /// <summary>
    /// 全船某个时间段内的航程
    /// </summary>
    /// <param name="vesselId"></param>
    /// <param name="startDateTimeUtc"></param>
    /// <param name="endDateTimeUtc"></param>
    /// <returns></returns>
    public async Task<float> GetDistanceForDateTimePeriodAsync(Guid vesselId, DateTime startDateTimeUtc, DateTime endDateTimeUtc)
    {
        // 计算航程不需要向前扩展一个采集周期
        
        // 获取船舶时间段内的数据
        var vessels = await _gpsTdRepository.GetListCloseOpenAscAsync(vesselId, startDateTimeUtc, endDateTimeUtc);
        return vessels.Sum(v => v.Distance);
    }
}